<?php
class ProductAction extends BaseAction
{
	function index()
	{
		$map = "1=1 ";
		if($_GET['nid']) $map .=" and nid=".$_GET['nid'];
		if($_GET['fid']) $map .=" and categoryID=".$_GET['fid'];
		
		$Dao = D("Product"); 
		$count = $Dao->count($map); 
		import("ORG.Util.Page"); 
		if(!empty($_REQUEST["listRows"])) { 
			$listRows = $_REQUEST["listRows"]; 
			}else{ 
			$listRows = 20; 
		} 
		$p = new Page($count,$listRows); 
 
		$field = "*"; 
		$list = $Dao->findAll($map,$field,"id desc",$p->firstRow.",".$p->listRows); 
		//dump($Dao->getLastSql());
		//dump($list);
		$page = $p->show(); 
		$this->assign("list",$list); 
		$this->assign("page",$page); 

		unset($Dao,$count,$page);
		//分类
		$Dao = D('Category');
        $nid = $_GET['nid'];
        if(!$nid) $this->error('nid参数丢失!');
		$list =  $Dao->where("moduleID=4 and nid=$nid")->order('orderNum asc')->findAll();
		$list = $Dao->toTree($list,'id','fid','_child');
		$this->treetoary($list);
		$this->category = $this->tree;
        //dump($Dao->getLastSql());
		$this->display(); 
	}

	function add() 
	{ 
		$Dao = D("Category"); 
        $nid = $_GET['nid'];
        if(!$nid) $this->error('nid参数丢失!');
		$list = $Dao->where("moduleID=4 and nid=$nid")->order('orderNum asc')->findAll();
		$list = $Dao->toTree($list,'id','fid','_child');
		$this->treetoary($list);
		$this->category = $this->tree;
		//dump($Dao->getLastSql());
		// 添加内容的页面 
		$this->display(); 
	} 

	function insert() 
	{ 
		// 保存添加的数据
		$Dao = D("Product"); 
		$vo = $Dao->create(); 
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
		//dump($vo);
		//dump($_FILES['file1']);
 		if(!empty($_FILES['file1']['name'])){
            //添加附件
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize  = 32922000 ;
            $upload->allowExts  = explode(',','jpg,gif');
            $upload->savePath = './Public/file/';

            //执行上传操作
            $result = $upload->upload();
            if(!$result){
                $this->error("上传出错! ".$upload->getErrorMsg());
            }
            $result = $upload->getUploadFileInfo();
            $file	=	$result[0]['savepath'].$result[0]['savename'];
            //dump($file);
			//dump($result[0]);
			$DaoAttach = D("Sysattach"); 
			$vo2 = $DaoAttach->create($result[0]); 
			if(false === $vo2) { 
				$this->error($DaoAttach->getError()); 
			} 
			$rs = $DaoAttach->add(); 
			
			include "./Public/sImage.class.php";
            $img = new sImage();
            $w=170;$h=115;
            //$file_s    = './Public/file/s_'.$result[0]['savename'];
            $file_s    = './Public/file/'.$result[0]['savename'];
            $img->cutThumb($file,$file_s,$w,$h);			
            $Dao->imgUrl = $file_s;//赋值到LINK表logoURL字段
			//echo ('上次成功!'.$file_s);
		}		
		$rs = $Dao->add(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__."/index/nid/".$vo['nid']); 
			$this->success("添加数据成功!"); 
		}else{ 
			$this->error("添加数据失败"); 
		} 
	}

	function edit() 
	{ 

		unset($Dao);
		// 编辑页面 
		$Dao = D("Product"); 
		$vo = $Dao->find("id=".$_GET["id"]); 
		if($vo === false) 
		{ 
			$this->assign("jumpUrl",__URL__."/index/nid/".$vo['nid']); 
			$this->error("Non-existed record"); 
		} 
		$this->assign("vo",$vo); 
		//dump($vo);
		unset($Dao);
		//
		$Dao = D("Category"); 
        $nid = $vo['nid'];
        if(!$nid) $this->error('nid参数丢失!');
		$list = $Dao->where("moduleID=4 and nid=$nid")->order('orderNum asc')->findAll();
		$list = $Dao->toTree($list,'id','fid','_child');
		$this->treetoary($list);
		$this->category = $this->tree;
		//dump($Dao->getLastSql());
		
		$this->display(); 
	} 

	function update() 
	{ 
		// 保存编辑过的数据 
		$Dao = D("Product"); 
		$vo = $Dao->create("","edit");
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 		if(!empty($_FILES['file1']['name'])){
			if($Dao->imgUrl) unlink($Dao->imgUrl); 
            //添加附件
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize  = 32922000 ;
            $upload->allowExts  = explode(',','jpg,gif');
            $upload->savePath = './Public/file/';

            //执行上传操作
            $result = $upload->upload();
            if(!$result){
                $this->error("上传出错! ".$upload->getErrorMsg());
            }
            $result = $upload->getUploadFileInfo();
            $file	=	$result[0]['savepath'].$result[0]['savename'];
            //dump($file);
			//dump($result[0]);
			$DaoAttach = D("Sysattach"); 
			$vo = $DaoAttach->create($result[0]); 
			if(false === $vo) { 
				$this->error($DaoAttach->getError()); 
			} 
	 
			$rs = $DaoAttach->add(); 
			include "./Public/sImage.class.php";
            $img = new sImage();
            $w=170;$h=115;
            //$file_s    = './Public/file/s_'.$result[0]['savename'];
            $file_s    = './Public/file/'.$result[0]['savename'];
            $img->cutThumb($file,$file_s,$w,$h);			
            $Dao->imgUrl = $file_s;//赋值到LINK表logoURL字段
			//echo ('上次成功!'.$file_s);
		}
		$rs = $Dao->save(); 
		if($rs){ 
				$this->assign("jumpUrl",__URL__."/index/nid/".$vo['nid']); 
			$this->success("保存修改成功!"); 
		}else{ 
			$this->error("保存修改失败"); 
		} 
	}

	function delete() 
	{ 
		// 根据id删除指定的记录
		$Dao = D("Product"); 
		$id = $_REQUEST["id"]; 
		if(isset($id)) { 
			$result = $Dao->getById($id); 
			if(!$result) { 
				$this->error("Non-existed record!"); 
			}
			if($Dao->imgUrl) unlink($Dao->imgUrl); 
			if($Dao->deleteById($id)){ 
				$this->assign("jumpUrl",__URL__."/index/nid/".$result['nid']); 
				$this->success("删除数据成功"); 
			}else { 
				$this->error("删除数据失败"); 
			}
		}else { 
			$this->error("非法操作"); 
		} 
	}
	
		var $tree=array();
	function treetoary($list,$level=0) {
	  foreach($list as $key=>$val)
	  {
		$tmp_str=str_repeat("&nbsp;",$level*2);
		if($level>0) {
			$tmp_str.="├";
		}
		$val['name']=$tmp_str."&nbsp;".$val['name'];
		if(!array_key_exists('_child',$val)) {
		   array_push($this->tree,$val);
		}else{
			$tmp_ary = $val['_child'];
			unset($val['_child']);
			array_push($this->tree,$val);
			$this->treetoary($tmp_ary,$level+1);
		}
	  }
	  return;
	}

}
?>